window.baidu = window.baidu || {
    /** @ignore */
    version:"1-0-0",
    /** @function */
    emptyFn:function(){}
};


/**
 * \u63d0\u4f9b\u4e86\u521b\u5efa\u4e0e\u83b7\u53d6flash\u5bf9\u8c61\u7684\u4e00\u7cfb\u5217\u65b9\u6cd5
 * @name baidu.swf
 * @namespace
 */
baidu.swf = baidu.swf || {};



baidu.isString = function(e){
    return (typeof e  == "object" && e && e.constructor == String )  || typeof e == "string";
};


/**
 * \u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684flash\u63d2\u4ef6\u7248\u672c\u53f7
 * 
 * @return {String} flash\u63d2\u4ef6\u7248\u672c\u53f7
 */
baidu.swf.getVersion = function () {
    var n = navigator;
    if (n.plugins && n.mimeTypes.length) {
        var a = n.plugins["Shockwave Flash"];
        if (a && a.description) {
            return a.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s)+r/, ".") + ".0";
        }
    } else if (window.ActiveXObject && !window.opera) {
        for (var i = 10; i >= 2; i--) {
            try {
                var c = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + i);
                if (c) {
                    return i + ".0.0";
                    break;
                }
            } catch(e) {}
        }
    }
};





/**
 * \u521b\u5efa\u4e00\u4e2aflash\u5bf9\u8c61\u7684html\u5b57\u7b26\u4e32
 * 
 * @param {Object} options \u521b\u5efaflash\u7684\u9009\u9879\u53c2\u6570
 * @config {String} id \u8981\u521b\u5efa\u7684flash\u7684\u6807\u8bc6
 * @config {String} url flash\u6587\u4ef6\u7684url
 * @config {String} ver \u6700\u4f4e\u9700\u8981\u7684flash player\u7248\u672c\u53f7
 * @config {String} width flash\u7684\u5bbd\u5ea6
 * @config {String} height flash\u7684\u9ad8\u5ea6
 * @config {String} align flash\u7684\u5bf9\u9f50\u65b9\u5f0f
 * @config {String} base \u8bbe\u7f6e\u7528\u4e8e\u89e3\u6790swf\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u76f8\u5bf9\u8def\u5f84\u8bed\u53e5\u7684\u57fa\u672c\u76ee\u5f55\u6216URL
 * @config {String} bgcolor swf\u6587\u4ef6\u7684\u80cc\u666f\u8272
 * @config {String} salign \u7f29\u653e\u7684swf\u6587\u4ef6\u5728\u7531width\u548cheight\u8bbe\u7f6e\u5b9a\u4e49\u7684\u533a\u57df\u5185\u7684\u4f4d\u7f6e
 * @config {String} menu \u662f\u5426\u663e\u793a\u53f3\u952e\u83dc\u5355
 * @config {String} loop \u64ad\u653e\u5230\u6700\u540e\u4e00\u5e27\u65f6\u662f\u5426\u91cd\u65b0\u64ad\u653e
 * @config {String} play flash\u662f\u5426\u5728\u6d4f\u89c8\u5668\u52a0\u8f7d\u65f6\u5c31\u5f00\u59cb\u64ad\u653e
 * @config {String} quality \u8bbe\u7f6eflash\u64ad\u653e\u7684\u753b\u8d28
 * @config {String} scale \u8bbe\u7f6eflash\u5185\u5bb9\u5982\u4f55\u7f29\u653e\u6765\u9002\u5e94\u8bbe\u7f6e\u7684\u5bbd\u9ad8
 * @config {String} wmode flash\u7684\u663e\u793a\u6a21\u5f0f
 * @config {String} allowscriptaccess flash\u4e0e\u9875\u9762\u7684\u901a\u4fe1\u6743\u9650
 * @config {String} allownetworking \u8bbe\u7f6eswf\u6587\u4ef6\u4e2d\u5141\u8bb8\u4f7f\u7528\u7684\u7f51\u7edcAPI
 * @config {String} allowfullscreen \u662f\u5426\u5141\u8bb8flash\u5168\u5c4f
 * @config {String} seamlesstabbing \u5141\u8bb8\u8bbe\u7f6e\u6267\u884c\u65e0\u7f1d\u8df3\u683c\uff0c\u4ece\u800c\u4f7f\u7528\u6237\u80fd\u8df3\u51faflash\u5e94\u7528\u7a0b\u5e8f\u3002\u8be5\u53c2\u6570\u53ea\u80fd\u5728\u5b89\u88c5Flash7\u53ca\u66f4\u9ad8\u7248\u672c\u7684Windows\u4e2d\u4f7f\u7528
 * @config {String} devicefont \u9759\u6001\u6587\u672c\u5bf9\u8c61\u662f\u5426\u4ee5\u8bbe\u5907\u5b57\u4f53\u5448\u73b0
 * @config {String} swliveconnect \u7b2c\u4e00\u6b21\u52a0\u8f7dflash\u65f6\u6d4f\u89c8\u5668\u662f\u5426\u5e94\u542f\u52a8Java
 * @config {Object} vars \u8981\u4f20\u9012\u7ed9flash\u7684\u53c2\u6570
 * @return {String} flash\u5bf9\u8c61\u7684html\u5b57\u7b26\u4e32
 */
baidu.swf.createHTML = function (options) {
    options = options || {};
    var version = baidu.swf.getVersion(), allowCreate = 1;
    var needVersion = options['ver'] || '6.0.0', vUnit1, vUnit2;

    if (version) {
        version = version.split('.');
        needVersion = needVersion.split('.');
        for (var i = 0; i < 3; i++) {
        	vUnit1 = parseInt(version[i], 10);
        	vUnit2 = parseInt(needVersion[i], 10);
            if (vUnit2 < vUnit1) {
                break;
            } else if (vUnit2 > vUnit1) {
                allowCreate = 0;
                break;
            }
        }
    } else {
        allowCreate = 0;
    }
    
    if (!allowCreate) {
        return '';
    }
    
    var vars = options['vars'];
    var i, k, len, item;
    var objProperties = ['classid', 'codebase', 'id', 'width', 'height', 'align'];
    
    // \u521d\u59cb\u5316object\u6807\u7b7e\u9700\u8981\u7684classid\u3001codebase\u5c5e\u6027\u503c
    options['align']    = options['align'] || 'middle';
    options['classid']  = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
    options['codebase'] = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0';
    options['movie']    = options['url'] || '';
    delete options['vars'];
    delete options['url'];
    
    // \u521d\u59cb\u5316flashvars\u53c2\u6570\u7684\u503c
    if (baidu.isString(vars)) {
        options['flashvars'] = vars;
    } else {
        var fvars = [];
        for (k in vars) {
            item = vars[k];
            if (item) {
                fvars.push(k + "=" + encodeURIComponent(item));
            }
        }
        options['flashvars'] = fvars.join('&');
    }
    
    // \u6784\u5efaIE\u4e0b\u652f\u6301\u7684object\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u5c5e\u6027\u548c\u53c2\u6570\u5217\u8868
    var str = ['<object '];
    for (i = 0, len = objProperties.length; i < len; i++) {
        item = objProperties[i];
        str.push(' ', item, '="', options[item], '"');
    }
    str.push('>');
    var params = {
        'wmode'             : 1,
        'scale'             : 1,
        'quality'           : 1,
        'play'              : 1,
        'loop'              : 1,
        'menu'              : 1,
        'salign'            : 1,
        'bgcolor'           : 1,
        'base'              : 1,
        'allowscriptaccess' : 1,
        'allownetworking'   : 1,
        'allowfullscreen'   : 1,
        'seamlesstabbing'   : 1,
        'devicefont'        : 1,
        'swliveconnect'     : 1,
        'flashvars'         : 1,
        'movie'             : 1
    };
    
    for (k in options) {
    	item = options[k];
        if (params[k] && item) {
            str.push('<param name="' + k + '" value="' + item + '" />');
    	}
    }
    
    // \u4f7f\u7528embed\u65f6\uff0cflash\u5730\u5740\u7684\u5c5e\u6027\u540d\u662fsrc\uff0c\u5e76\u4e14\u8981\u6307\u5b9aembed\u7684type\u548cpluginspage\u5c5e\u6027
    options['src']  = options['movie'];
    options['name'] = options['id'];
    delete options['id'];
    delete options['movie'];
    delete options['classid'];
    delete options['codebase'];
    options['type']        = 'application/x-shockwave-flash';
    options['pluginspage'] = 'http://www.macromedia.com/go/getflashplayer';
    
    
    // \u6784\u5efaembed\u6807\u7b7e\u7684\u5b57\u7b26\u4e32
    str.push('<embed');
    // \u5728firefox\u3001opera\u3001safari\u4e0b\uff0csalign\u5c5e\u6027\u5fc5\u987b\u5728scale\u5c5e\u6027\u4e4b\u540e\uff0c\u5426\u5219\u4f1a\u5931\u6548
    // \u7ecf\u8fc7\u8ba8\u8bba\uff0c\u51b3\u5b9a\u91c7\u7528BT\u65b9\u6cd5\uff0c\u628ascale\u5c5e\u6027\u7684\u503c\u5148\u4fdd\u5b58\u4e0b\u6765\uff0c\u6700\u540e\u8f93\u51fa
    var salign;
    for (k in options) {
        item = options[k];
        if (item) {
        	if ((new RegExp("^salign\x24", "i")).test(k)) {
                salign = item;
        		continue;
        	}
        	
            str.push(' ', k, '="', item, '"');
        }
    }
    
    if (salign) {
    	str.push(' salign="', salign, '"');
    }
    str.push('></embed></object>');
    
    return str.join('');
};


/**
 * \u5728\u9875\u9762\u4e2d\u521b\u5efa\u4e00\u4e2aflash\u5bf9\u8c61\u3002
 * <pre>
 * \u63d0\u4f9b\u4e862\u79cd\u521b\u5efa\u65b9\u5f0f\uff1a
 * 1. \u5728\u4ee3\u7801\u5f53\u524d\u4f4d\u7f6e\u521b\u5efa
 * 2. \u521b\u5efa\u5230\u4e00\u4e2a\u7236\u5bb9\u5668\u5143\u7d20\u4e2d
 * </pre>
 * 
 * @param {Object} options \u521b\u5efaflash\u7684\u9009\u9879\u53c2\u6570
 * @config {String} id \u8981\u521b\u5efa\u7684flash\u7684\u6807\u8bc6
 * @config {String} url flash\u6587\u4ef6\u7684url
 * @config {String} errorMessage \u672a\u5b89\u88c5flash player\u6216flash player\u7248\u672c\u53f7\u8fc7\u4f4e\u65f6\u7684\u63d0\u793a
 * @config {String} ver \u6700\u4f4e\u9700\u8981\u7684flash player\u7248\u672c\u53f7
 * @config {String} width flash\u7684\u5bbd\u5ea6
 * @config {String} height flash\u7684\u9ad8\u5ea6
 * @config {String} align flash\u7684\u5bf9\u9f50\u65b9\u5f0f
 * @config {String} base \u8bbe\u7f6e\u7528\u4e8e\u89e3\u6790swf\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u76f8\u5bf9\u8def\u5f84\u8bed\u53e5\u7684\u57fa\u672c\u76ee\u5f55\u6216URL
 * @config {String} bgcolor swf\u6587\u4ef6\u7684\u80cc\u666f\u8272
 * @config {String} salign \u7f29\u653e\u7684swf\u6587\u4ef6\u5728\u7531width\u548cheight\u8bbe\u7f6e\u5b9a\u4e49\u7684\u533a\u57df\u5185\u7684\u4f4d\u7f6e
 * @config {String} menu \u662f\u5426\u663e\u793a\u53f3\u952e\u83dc\u5355
 * @config {String} loop \u64ad\u653e\u5230\u6700\u540e\u4e00\u5e27\u65f6\u662f\u5426\u91cd\u65b0\u64ad\u653e
 * @config {String} play flash\u662f\u5426\u5728\u6d4f\u89c8\u5668\u52a0\u8f7d\u65f6\u5c31\u5f00\u59cb\u64ad\u653e
 * @config {String} quality \u8bbe\u7f6eflash\u64ad\u653e\u7684\u753b\u8d28
 * @config {String} scale \u8bbe\u7f6eflash\u5185\u5bb9\u5982\u4f55\u7f29\u653e\u6765\u9002\u5e94\u8bbe\u7f6e\u7684\u5bbd\u9ad8
 * @config {String} wmode flash\u7684\u663e\u793a\u6a21\u5f0f
 * @config {String} allowscriptaccess flash\u4e0e\u9875\u9762\u7684\u901a\u4fe1\u6743\u9650
 * @config {String} allownetworking \u8bbe\u7f6eswf\u6587\u4ef6\u4e2d\u5141\u8bb8\u4f7f\u7528\u7684\u7f51\u7edcAPI
 * @config {String} allowfullscreen \u662f\u5426\u5141\u8bb8flash\u5168\u5c4f
 * @config {String} seamlesstabbing \u5141\u8bb8\u8bbe\u7f6e\u6267\u884c\u65e0\u7f1d\u8df3\u683c\uff0c\u4ece\u800c\u4f7f\u7528\u6237\u80fd\u8df3\u51faflash\u5e94\u7528\u7a0b\u5e8f\u3002\u8be5\u53c2\u6570\u53ea\u80fd\u5728\u5b89\u88c5Flash7\u53ca\u66f4\u9ad8\u7248\u672c\u7684Windows\u4e2d\u4f7f\u7528
 * @config {String} devicefont \u9759\u6001\u6587\u672c\u5bf9\u8c61\u662f\u5426\u4ee5\u8bbe\u5907\u5b57\u4f53\u5448\u73b0
 * @config {String} swliveconnect \u7b2c\u4e00\u6b21\u52a0\u8f7dflash\u65f6\u6d4f\u89c8\u5668\u662f\u5426\u5e94\u542f\u52a8Java
 * @config {Object} vars \u8981\u4f20\u9012\u7ed9flash\u7684\u53c2\u6570
 * @param {HTMLElement|String} target \u521b\u5efaflash\u7684\u7236\u5bb9\u5668\u5143\u7d20
 * @return {Boolean} \u662f\u5426\u6210\u529f\u521b\u5efa\u4e86flash\u5bf9\u8c61
 */
baidu.swf.create = function (options, target) {
    options = options || {};
    var html = baidu.swf.createHTML(options);
    var returnValue = true;
    if (target && baidu.isString(target)) {
        target = document.getElementById(target);
    }
    
    if (html.length <= 0) {
        html = options['errorMessage'] || '';
        returnValue = false;
    }
    
    if (target) {
        target.innerHTML = html;
    } 
    
    return returnValue;
};
 
/**
 * \u83b7\u53d6\u9875\u9762\u4e2d\u7684flash\u5bf9\u8c61
 * 
 * @param {String} movieName \u8981\u83b7\u53d6\u7684flash\u5bf9\u8c61\u7684\u540d\u79f0\u3002\u5728Object\u4e2d\u4e3aid\uff0cembed\u4e2d\u4e3aname\u3002
 * @return {HTMLElement} flash\u5bf9\u8c61
 */
baidu.swf.getMovie = function (movieName) {
    return document[movieName] || window[movieName];
};
